package br.net.woodstock.tickettoride.api.jee6.core.impl;

import java.util.Collection;
import java.util.Map;

import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;

import br.net.woodstock.rockframework.persistence.orm.QueryMetadata;
import br.net.woodstock.rockframework.persistence.orm.QueryResult;
import br.net.woodstock.rockframework.persistence.orm.impl.JPAQueryableRepository;
import br.net.woodstock.rockframework.persistence.orm.impl.QueryMetadataImpl;
import br.net.woodstock.tickettoride.api.core.SimpleQueryableRepository;

@Stateless(name = "simpleQueryableRepository")
public class SimpleQueryableRepositoryImpl implements SimpleQueryableRepository {

	private static final long	serialVersionUID	= -3139155797801102518L;

	@PersistenceContext(name = "defaultPU", type = PersistenceContextType.TRANSACTION)
	private EntityManager		entityManager;

	@Override
	@TransactionAttribute(TransactionAttributeType.SUPPORTS)
	public <E> E getSingle(final String jpql, final Map<String, Object> parameters) {
		QueryMetadata metadata = new QueryMetadataImpl(jpql, parameters, null);
		return this.getSingle(metadata);
	}

	@Override
	@TransactionAttribute(TransactionAttributeType.SUPPORTS)
	public <E> E getSingle(final String jpql, final Map<String, Object> parameters, final Map<String, Object> options) {
		QueryMetadata metadata = new QueryMetadataImpl(jpql, parameters, options);
		return this.getSingle(metadata);
	}

	@Override
	@TransactionAttribute(TransactionAttributeType.SUPPORTS)
	public <E> Collection<E> getCollection(final String jpql, final Map<String, Object> parameters) {
		QueryMetadata metadata = new QueryMetadataImpl(jpql, parameters, null);
		return this.getCollection(metadata).getResult();
	}

	@Override
	@TransactionAttribute(TransactionAttributeType.SUPPORTS)
	public <E> Collection<E> getCollection(final String jpql, final Map<String, Object> parameters, final Map<String, Object> options) {
		QueryMetadata metadata = new QueryMetadataImpl(jpql, parameters, options);
		return this.getCollection(metadata).getResult();
	}

	@Override
	@TransactionAttribute(TransactionAttributeType.REQUIRED)
	public void executeUpdate(final String jpql, final Map<String, Object> parameters) {
		QueryMetadata metadata = new QueryMetadataImpl(jpql, parameters, null);
		this.executeUpdate(metadata);
	}

	@Override
	@TransactionAttribute(TransactionAttributeType.REQUIRED)
	public void executeUpdate(final String jpql, final Map<String, Object> parameters, final Map<String, Object> options) {
		QueryMetadata metadata = new QueryMetadataImpl(jpql, parameters, options);
		this.executeUpdate(metadata);
	}

	@Override
	@TransactionAttribute(TransactionAttributeType.SUPPORTS)
	public <E> E getSingle(final QueryMetadata query) {
		return new JPAQueryableRepository(this.entityManager).getSingle(query);
	}

	@Override
	@TransactionAttribute(TransactionAttributeType.SUPPORTS)
	public QueryResult getCollection(final QueryMetadata query) {
		return new JPAQueryableRepository(this.entityManager).getCollection(query);
	}

	@Override
	@TransactionAttribute(TransactionAttributeType.REQUIRED)
	public void executeUpdate(final QueryMetadata query) {
		new JPAQueryableRepository(this.entityManager).executeUpdate(query);
	}

}